﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressUserBusinessLayer;
using System.Data;

namespace AlexandriaPressWebsite.MasterPages.UserPages
{
    public partial class LoggedInUserPage : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            UserBusinsessLayer business = new UserBusinsessLayer();
            string _UserType = business.getUserType(HttpContext.Current.User.Identity.Name);
            if (_UserType == "Type_Buyer" || _UserType == "Type_Admin" || _UserType == "Type_ProofReader" || _UserType == "Type_Author")
            {

            }
            else
            {
                Response.Redirect("~/MasterPages/ErrorPage.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            UserBusinsessLayer user = new UserBusinsessLayer();
            Session.Add("userID", user.getUser(HttpContext.Current.User.Identity.Name).UserID);
            usernameLabel.Text = HttpContext.Current.User.Identity.Name;
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                
                if (HttpContext.Current.User.Identity.Name != "")
                {
                    UserBusinsessLayer business = new UserBusinsessLayer();
                    Session.Add("userType", business.getUserType(HttpContext.Current.User.Identity.Name));

                    DateTime suspension = user.checkIfSuspended(Convert.ToInt32(Session["userID"]));
                    if (suspension > DateTime.Now)
                    {
                        Session.Add("blocked", "User_Blocked");
                        Response.Redirect("~/MasterPages/MainPage.aspx");
                    }
                    else
                    {
                        int banned = user.checkIfBanned(Convert.ToInt32(Session["userID"]));
                        if (banned > 0)
                        {
                            Session.Add("blocked", "User_Blocked");
                            Response.Redirect("~/MasterPages/MainPage.aspx");
                        }
                        else
                        {
                            DataTable titles = user.searchTitleView(user.GetLatestBooks());
                            searchGridView1.DataSource = titles;
                            searchGridView1.EmptyDataText = "No books to display.";
                            searchGridView1.DataBind();
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/MasterPages/MainPage.aspx");
                }
            }
            else
            {
                Response.Redirect("~/MasterPages/MainPage.aspx");
            }
            

            
        }

        protected void searchGridView1_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            UserBusinsessLayer user = new UserBusinsessLayer();
                searchGridView1.PageIndex = e.NewPageIndex;
                searchGridView1.DataSource = user.searchTitleView(user.GetLatestBooks());
                searchGridView1.DataBind();
        }
    }
}
